package com.apes.fn.server.productReturn;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.fn.server.productReceive.ProductReceiveItem;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;


@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ProductReturnItem.CONST_TABLE_NAME)
public class ProductReturnItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "stock_product_return_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "归还单明细")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "product_return_id", referencedColumnName = "id", nullable = false)
    @Comment("归还单号")
    private ProductReturn productReturn;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_receive_item_id", referencedColumnName = "id")
    @Comment("领用单明细编号")
    private ProductReceiveItem productReceiveItem;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;


    @Transient
    private  String  carLicense;

    public String getCarLicense() {
        if( productReceiveItem == null ||
                productReceiveItem.queryCarLicense() == null ) return null;
        return productReceiveItem.queryCarLicense();
    }

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
//    @Comment("供应商")
//    private PartyRole supplier;
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "purchase_org_id", referencedColumnName = "id")
//    @Comment("采购组织编码")
//    private Organization purchaseOrg;  //采购组织

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    @Comment("商品")
    private Product product;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id", nullable = false)
    @Comment("计量单位")
    private ProductUom uom;

//    @NotNull
//    @Column(name = "SB", columnDefinition = "number(2) default 0")
//    @Comment("税别（0：增值税；1：营业税）")
//    private int SB;
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "tax_id", referencedColumnName = "id", nullable = false)
//    @Comment("税率")
//    private Tax tax;

    @Column(name = "qty_return", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("归还数量")
    private double qtyReturn = 0;

    @Column(name = "remark", length = 120)
    @Comment("备注")
    private String remark;

    @Transient
    private Double qtyReturnable; // 可归还数量

    public Double getQtyReturnable() {
        if( "approve".equals(productReturn.getState()) || productReceiveItem == null ) return null;
        return productReceiveItem.getQtyReceive() - productReceiveItem.getQtyReturned();
    }

    //领用单ID
    @Transient
    private String productReceiveId;

    public String getProductReceiveId() {
        if (productReceiveItem == null) return null;
        return productReceiveItem.getProductReceive().getId();
    }

}
